/*
 * Copyright 2017-2020 Wuwei Tengyu Logistics Center Co., Ltd.
 */

package com.goblin.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * desc: date util.
 *
 * @author go blin
 * <p>
 * 2019/2/26 10:03
 */
public final class DateUtil {

	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
	public static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
	public static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");
	public static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat sdf6 = new SimpleDateFormat("E");
	public static SimpleDateFormat sdf7 = new SimpleDateFormat("dd");
	public static SimpleDateFormat sdf8 = new SimpleDateFormat("yyyyMM");
	public static SimpleDateFormat sdf9 = new SimpleDateFormat("HHmmss");

	private DateUtil() {
	}

	public static Date stringTransferDate(String dateString) {
		if (dateString != null) {
			try {
				return sdf.parse(dateString);
			}
			catch (ParseException ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	public static Date stringTransferDateP(String dateString, SimpleDateFormat sdf) {
		if (dateString != null) {
			try {
				return sdf.parse(dateString);
			}
			catch (ParseException ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	public static String dateTransferString(Date date) {
		if (date != null) {
			try {
				return sdf.format(date);
			}
			catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	//s1格式转换为s2格式
	public static String stringDateTransferString(String d1, SimpleDateFormat s1,
			SimpleDateFormat s2) {
		if (s1 == null || s2 == null || d1 == null) {
			return null;
		}
		try {
			return s2.format(s1.parse(d1));
		}
		catch (ParseException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public static String dateTransferString(Timestamp date) {
		if (date != null) {
			try {
				return sdf.format(date);
			}
			catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	public static String dateTransferString2(Timestamp date) {
		if (date != null) {
			try {
				return sdf2.format(date);
			}
			catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	public static String getLatelyDate() {
		return sdf.format(new Date());
	}

	public static String getLatelyDateP(SimpleDateFormat sdf) {
		return sdf.format(new Date());
	}

	public static boolean validateDateFormatP(String date, SimpleDateFormat sdf) {
		if (date == null || sdf == null) {
			return false;
		}
		sdf.setLenient(false);
		try {
			sdf.parse(date);
		}
		catch (ParseException ex) {
			return false;
		}
		return true;
	}

	public static String formatGsLatelyDate(Date date) {
		return sdf3.format(date);
	}

	public static String formatDate(Date date, SimpleDateFormat sdf) {
		return sdf.format(date);
	}

	public static Timestamp transferDateToStamp(String date) throws ParseException {
		return new Timestamp(Long.parseLong(String.valueOf(sdf.parse(date).getTime())));

	}

	public static String gainToDayBefore() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);
		date = calendar.getTime();
		return sdf.format(date);
	}

	public static String gainLastMonsYM() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);

		return sdf1.format(calendar.getTime());
	}

	public static Integer gainLastMonsDaysOfMons() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public static String getLatelyDate4File() {
		return sdf5.format(new Date());
	}

	/**
	 * @param typeField 指定日期类型， 7.指周类型， 5.指月类型
	 * @desc: 获取指定date的指定类型日期集合.
	 * @author goblin
	 * @date 2020/8/24 10:34
	 */
	public static List<Date> gainWholePeriod(Date date, int typeField) {
		if (date == null) {
			return null;
		}
		if (typeField != 7 && typeField != 5) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		int today = calendar.get(typeField);
		int size = calendar.getMaximum(typeField) - 1;
		calendar.setTime(date);
		today = typeField == 7 ? today - 1 : today;
		List<Date> list = new ArrayList<>();
		calendar.add(typeField, 1 - today);
		list.add(calendar.getTime());
		for (int i = 0; i < size; i++) {
			calendar.add(typeField, 1);
			list.add(calendar.getTime());
		}
		return list;
	}

}
